﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace UMLDesigner
{
    static class WindowsManager
    {
        private static MainWindow mainWindow;
        private static FormControl formControl;

        private static List<RelationView> relations = new List<RelationView>();
        private static List<ClassView> classes = new List<ClassView>();

        private static ClassView selectedClass = null;
        private static RelationView relationToCreate = null;
              
        
        public static Form Build() {
            mainWindow = new MainWindow();
            formControl = mainWindow.GetControlFormulario();

            return mainWindow;
        }

        public static List<RelationView> GetRelations()
        {
            return relations;
        }

        public static List<RelationData> GetRelationOfClass(ClassData classData)
        {
           
            List<RelationData> relationsOfClass = new List<RelationData>();
            
                foreach (RelationView rel in relations)
                {
                    if (rel.GetRelationData().GetRelationType() == RelationData.RelationType.Inheritance)
                    {
                        if (rel.GetIniClass().GetClassData() == classData)
                        {
                            relationsOfClass.Add(rel.GetRelationData());
                        }
                    }
                    else if (rel.GetEndClass().GetClassData() == classData)
                    {
                        relationsOfClass.Add(rel.GetRelationData());
                    }
                    
                }

            return relationsOfClass;
        }

        public static RelationView GetRelation(int index)
        {
            return relations[index];
        }


        public static void AddRelations(RelationView rel, ClassView iniClass, ClassView endClass)
        {
            if (rel is InheritanceView)
            {
                rel.SetIniClass(iniClass);
                rel.SetEndClass(endClass);
            }
            else
            {
                rel.SetIniClass(endClass);
                rel.SetEndClass(iniClass);
            }
            relations.Add(rel);
           
            mainWindow.PaintRelations();
        }

        public static void FormRefresh(ClassView classView) {
            formControl.Refresh(classView);
        }

        public static void SelectClass(ClassView classView)
        {
            if (relationToCreate != null && selectedClass!=null)
            {
                AddRelations(relationToCreate, selectedClass, classView);
                relationToCreate = null;
            }

            DeselectClass();
            selectedClass = classView;
            selectedClass.Select();

            FormRefresh(classView);
        }

        public static void DeselectClass()
        {
            if (selectedClass != null)
            {
                selectedClass.Deselect();
                formControl.Deselect();
                selectedClass = null;
            }
        }

        public static void SetRelationToCreate(RelationView rel)
        {
            relationToCreate = rel;
        }

        public static void AddClass(ClassView clase)
        {
            classes.Add(clase);
        }

        public static void DeleteRelationship()
        {

        }
        public static void AddAttributeToForm(string name, string type, string visibility)
        {
            formControl.AddAttribute(name, type, visibility);
         
        }
        public static void ModifyAttribute(string name, string type, string visibility)
        {
            formControl.ModifyAttribute(name, type, visibility);
        }

        public static void AddMethodToForm(string visibility, string name, List<ParameterData> parameters, string returnType)
        {
            formControl.AddMethods(visibility, name, parameters, returnType);
        }

        public static void ModifyMethod(string visibility, string name, List<ParameterData> parameters, string returnType)
        {
            formControl.EditMethod(visibility, name, parameters, returnType);
        }

        public static void EditRelation(string name,string iniCard,string endCard, string iniAttr,string endAttr,string iniAttrVisi,string endAttrVisi)
        {
            formControl.EditRelation(name, iniCard, endCard, iniAttr, endAttr,iniAttrVisi,endAttrVisi);
           
        }

        public static void PaintRelations()
        {
            mainWindow.PaintRelations();
        }

        public static void DeleteClass()
        {
            classes.Remove(selectedClass);
            mainWindow.DeleteClass(selectedClass);
            DeselectClass();
        }
      
        public static void ExportToCode(string projectName)
        {
            foreach (ClassView classView in classes)
            {
                String name = classView.GetClassData().GetName();
                StreamWriter fileW = File.CreateText(name + ".cs");
                classView.GetClassData().ExportCode(fileW, "MyProject");
                fileW.Close();
            }
        }
    }
}
